import { TargetState } from "framework/FSM/DecisionTree/DecisionTargetState";
 
import { HFSMFactoryHelper } from "../HFSMFactoryHelper";
 
import { TeamUpgradeState } from "../../Control/State/team/TeamUpgradeState";
import { DecisionActive } from "../../Control/Decision/normal/DecisionActive";
import { State_MoveRetreat } from "../../Control/State/team/move/Combat/State_MoveRetreat";
import { State_MoveCloseTo } from "../../Control/State/team/move/Combat/State_MoveCloseTo";
import { State_CombatMove } from "../../Control/State/team/move/Combat/State_CombatMove";
import { SubMachineState } from "framework/FSM/SubMachineState";
import { DecisionIsCombatMove } from "../../Control/Decision/team/move/Combat/DecisionIsCombatMove";
import { DecisionTreeTransition } from "framework/FSM/Transition/DecisionTreeTransition";
import { State_LineMove } from "../../Control/State/team/move/State_LineMove";
import { State_Assemble } from "../../Control/State/team/move/State_Assemble";
import { DecisionIsAssemble } from "../../Control/Decision/team/move/DecisionIsAssemble";
import { DecisionIsTeamReadyCombat } from "../../Control/Decision/team/move/DecisionIsTeamReadyCombat";
import { DecisionIsLineMove } from "../../Control/Decision/team/move/DecisionIsLineMove";
import { DecisionIsTeamTargetDismiss } from "../../Control/Decision/team/move/DecisionIsTeamTargetDismiss";
import { DecisionIsTeam4MoveActive } from "../../Control/Decision/team/move/DecisionIsTeam4MoveActive";
import { TeamAttackState } from "../../Control/State/team/TeamAttackState";
import { DecisionTeamActive } from "../../Control/Decision/team/DecisionTeamActive";
import { State } from "framework/FSM/AState";
import { DecisionIsTeamExitCombat } from "../../Control/Decision/team/move/DecisionIsTeamExitCombat";
import { DecisionIsTeam2CombatMove } from "../../Control/Decision/team/move/Combat/DecisionIsTeam2CombatMove";

 
export class AITeamFactory
{
   
    // 升级小队
    static CreateTeamUpgrade()
    {
        return HFSMFactoryHelper.CreateHFSMModuleBy(DecisionTeamActive,new TeamUpgradeState());
    }
    

    static CreateTeam4()
    {
        return HFSMFactoryHelper.CreateHFSMModuleBy(DecisionTeamActive,this.CreateTeamAttack()).addComponents([
            this.CreateTeam4Move( AITeamFactory.CreateCombatMove())
        ]);
    }
    static CreateTeam2()
    {
        return HFSMFactoryHelper.CreateHFSMModuleBy(DecisionTeamActive,this.CreateTeamAttack()).addComponents([
            this.CreateTeam2Move(new State_CombatMove())
        ]);
    }
    /**
     * 小队激活攻击状态，抵达目的地，手动标记，被打之后。
     * @returns 
     */ 
    private static CreateTeamAttack()
    {
        return HFSMFactoryHelper.CreateHFSMModuleBy(DecisionActive,new TeamAttackState());
    }


    /**
     * 4人小队移动
     * @returns 
     */
    private static CreateTeam4Move(combatState:State)
    {
        const LineMove = new State_LineMove();
        const AssembleMove =  new State_Assemble();
        const CombatMove = combatState;
        const teammove = new SubMachineState([LineMove,AssembleMove,CombatMove]);
        const decision_Assemble = new DecisionIsAssemble(
            new DecisionIsTeamReadyCombat(
                new TargetState(CombatMove),
                new TargetState(LineMove)
            )
            );
        const decision_Linemove = new DecisionIsLineMove(
            new DecisionIsTeamReadyCombat(
                new TargetState(CombatMove)

            )
            ,new TargetState(AssembleMove) );
        const decision_CombatMove = new DecisionIsTeamTargetDismiss(
            new TargetState(AssembleMove),
            new DecisionIsTeamExitCombat(
                new TargetState(LineMove),
                undefined,
            )
           );
        AssembleMove.addTransition(new DecisionTreeTransition(decision_Assemble));
        LineMove.addTransition(new DecisionTreeTransition(decision_Linemove));
        CombatMove.addTransition(new DecisionTreeTransition(decision_CombatMove));
        return HFSMFactoryHelper.CreateHFSMModuleBy(DecisionIsTeam4MoveActive,teammove);
    }

     /**
     * 4人小队移动
     * @returns 
     */
     private static CreateTeam2Move(combatState:State)
     {
         const LineMove = new State_LineMove();
         const AssembleMove =  new State_Assemble();
         const CombatMove = combatState;
         const teammove = new SubMachineState([LineMove,AssembleMove,CombatMove]);
         const decision_Assemble = new DecisionIsAssemble(
             new DecisionIsTeam2CombatMove(
                 new TargetState(CombatMove),
                 new TargetState(LineMove)
             )
             );
         const decision_Linemove = new DecisionIsLineMove(
             new DecisionIsTeam2CombatMove(
                 new TargetState(CombatMove)
 
             )
             ,new TargetState(AssembleMove) );
         const decision_CombatMove = new DecisionIsTeamTargetDismiss(
             new TargetState(AssembleMove),
             new DecisionIsTeam2CombatMove(
                 undefined,
                 new TargetState(LineMove),
             )
            );
         AssembleMove.addTransition(new DecisionTreeTransition(decision_Assemble));
         LineMove.addTransition(new DecisionTreeTransition(decision_Linemove));
         CombatMove.addTransition(new DecisionTreeTransition(decision_CombatMove));
         return HFSMFactoryHelper.CreateHFSMModuleBy(DecisionIsTeam4MoveActive,teammove);
     }

    // 战斗小队移动
    // 暂时没写撤退条件
    private static CreateCombatMove()
    {
        const RetreatMove = new State_MoveRetreat();       // 撤退
        const CloseToMove =  new State_MoveCloseTo();   // 靠近
        const CombatMove =  new State_CombatMove();
        const SMS_combat = new SubMachineState([CloseToMove,CombatMove,RetreatMove]);

        const decision_closeto = new DecisionIsCombatMove(
            new TargetState(CombatMove),
        );
        const decision_combat= new DecisionIsCombatMove(
            undefined,
            new TargetState(CloseToMove)
        );

        CombatMove.addTransition(new DecisionTreeTransition(decision_combat));
        CloseToMove.addTransition(new DecisionTreeTransition(decision_closeto));

        return SMS_combat;
    }

    private static CreateTeam2CombatMove()
    {
        const RetreatMove = new State_MoveRetreat();       // 撤退
        const CombatMove =  new State_CombatMove();
        const SMS_combat = new SubMachineState([ CombatMove,RetreatMove]);

        const decision_retreat = new DecisionIsCombatMove(
            new TargetState(CombatMove),
        );
        const decision_combat= new DecisionIsCombatMove(
            undefined,
            new TargetState(CombatMove)
        );

        CombatMove.addTransition(new DecisionTreeTransition(decision_combat));
        RetreatMove.addTransition(new DecisionTreeTransition(decision_retreat));

        return SMS_combat;
    }
}